import { Net } from "./Net"
import { Storage } from "./Storage"
import { promiseBuild } from "./Build"
import { cSys } from "./Sys";
import { Jx } from "./Jx";

// 自动登录
export class Login{
    // 引导页
    static saveGuide(hide){
        Storage.save(Storage.NOT_GUIDE, hide ? "1" : "");
    }

    // 清除 token
    static clearToken(){
        Storage.save(Storage.SESSION, "");
    }

    static clearCache(){
        this.saveGuide(false);
        this.clearToken();
    }

    // 请求（是否安装微信）（版本检测）（是否过审状态）
    static promiseRegisterWeChatAndVersion(){
        return new Promise(resolve=>{
            promiseBuild().then(build=>{
                console.log(build.desc);
                Promise.all([
                    cSys.registerWeChat(build.appid), 
                    Net.fetVersion(build.versionName),
                ]).then(rs=>{
                    // resolve({ isWeChatRegistered:rs[0], versionUrl:rs[1].url, apply:rs[1].apply});
                    resolve({ isWeChatRegistered:rs[0], ...rs[1]});
                }).catch(err=>resolve({ isWeChatRegistered:false}))
            })
        });
    }

    static promiseLogin(){
        return new Promise(resolve=>{
            Promise.all([
                this.promiseRegisterWeChatAndVersion(),
                // 获取本地存储（手机号, 登录token, 是否显示引导页） 
                Storage.promiseLoad(Storage.ACCOUNT),
                Storage.promiseLoad(Storage.SESSION), 
                Storage.promiseLoad(Storage.NOT_GUIDE), 
            ]).then(rs=>{
                let obj = {
                    ...rs[0],
                    account:rs[1] || "",
                    token:rs[2],
                    guide:!rs[3],
                    // stringEmpty
                    tokenData:"",
                };
                console.warn(Jx.stringify(obj));
                console.log("微信注册: " + obj.isWeChatRegistered);
                console.log("版本链接: " + obj.url);
                console.log("过审状态: " + obj.applied);
                console.log("本地账号: " + obj.account);
                console.log("本地引导页: " + obj.guide);
                // obj.versionUrl || 
                if(obj.url || !obj.token){
                    // console.log("本地token: " + obj.token);
                    resolve(obj);
                    return;
                }

                // Dialog.loading(true);
                // Net.fetch("team/myRole", Jx.toPost(null, null, obj.token))
                Net.fetRole(obj.token)
                .then(r=>{
                    // Dialog.hiding();
                    if(r.success){      
                        Net._token = obj.token;
                        obj.tokenData = r.data;
                        console.log("本地token: " + Net._token);
                        resolve(obj);
                    }else{
                        obj.token = "";
                        console.log("本地token:");
                        this.clearToken();
                        resolve(obj)
                    }
                }).catch(err=>{
                    // Dialog.hiding();
                    obj.token = "";
                    console.log("本地token:");
                    this.clearToken();
                    resolve(obj)
                });
            })
        });
    }
}


// static test(a){
//     console.log("空参数: " + typeof(a));
//     if({}){
//         console.log("{} is true");
//     }else {
//         console.log("{} is false");
//     }
// 数组也算特殊的对象
//     if([]){
//         console.log("[] is true");
//     }else {
//         console.log("[] is false");
//     }
// }
